package spring.mvc.controller;

import javax.annotation.Resource;

import spring.mvc.domain.Board;
import spring.mvc.service.BoardService;
import spring.mvc.service.CommentService;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * Handles requests for the application home page.
 */
@Controller
public class BoardController {
	
	@Resource
	private BoardService BoardService;
	@Resource
	private CommentService CommentService;
	
	@RequestMapping(value = "/")
		public String main (Model model) throws Exception
	{
		model.addAttribute("value", BoardService.list());
		return "main";
	}

/*	 json 사용
	@RequestMapping(value = "/son")
	public @ResponseBody Map<String,Object> son(@RequestParam Map<String,Object> paramMap) throws Exception
	{
		paramMap.put("value", BoardService.list());
		return paramMap;
	}	
	
	@RequestMapping(value = "/son")
	
	public @ResponseBody List<Board> son2() throws Exception
	{
		List<Board> list = new ArrayList<Board>();
		list= BoardService.list();
		return list;
	}*/
	
	@RequestMapping(value = "/writeform")
	public String writeform() throws Exception
	{
		return "writeform";
	}
	
	@RequestMapping(value = "/write", method = RequestMethod.POST)
	public String write (@ModelAttribute("board") Board board) throws Exception
	{
		Integer seq = BoardService.write(board);
		return "redirect:/view?seq="+seq;
	}
	
	@RequestMapping(value = "/view", method = RequestMethod.GET)
	public String view (Model model, @RequestParam(value="seq") String seq) throws Exception
	{
		model.addAttribute("view",BoardService.view(seq));
		model.addAttribute("co_list",CommentService.list(seq));
		return "view";
	}
	
	@RequestMapping(value = "/updateform")
	public String updateform(@ModelAttribute("board") Board board, Model model) throws Exception
	{
		model.addAttribute("value",board);
		return "updateform";
	}
	
	@RequestMapping(value = "/update")
	public String update(@ModelAttribute("board") Board board, Model model) throws Exception
	{
		BoardService.update(board);
		return "redirect:/view?seq="+board.getSeq();
	}
	
	@RequestMapping(value = "/delete", method = RequestMethod.GET)
	public String delete(@RequestParam(value="seq") String seq) throws Exception
	{
		BoardService.delete(seq);
		return "redirect:/";
	}
	
	@RequestMapping(value = "/addReplyform", method = RequestMethod.POST)
	public String addReplyform(@ModelAttribute("board") Board board, Model model) throws Exception
	{
		model.addAttribute("value",board);
		return "addReplyform";
	}
	
	@RequestMapping(value = "/addReply", method = RequestMethod.POST)
	public String addReply(@ModelAttribute("board") Board board, Model model) throws Exception
	{
		BoardService.addReply(board);
		return "redirect:/";
	}

}
